package com.xisoft.devise.utils;

import freemarker.template.Template;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;

/**
 * *  代码生成器工具类
 * Created with Description:
 *
 * @fileRelativePath: GenUtils.java
 * @Date: Created on 2022/8/10  12:41  <br>
 * @author: LiBencheng <br>
 * @version: 1.0 <br>
 */
public class GenUtils
{

    /************************************************************* <br>
     *
     *说明: 列名转换成Java属性名<br>
     *
     * @Description   <br>
     * @Date Created on 2022/8/10  12:42  <br>
     * @param columnName <br>
     * @return java.lang.String  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public static String columnToJava(String columnName)
    { return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", ""); }

    /**
     * 获取模板渲染后的内容
     *
     * @param content   模板内容
     * @param dataModel 数据模型
     */
    public static String getTemplateContent(String content, Map< String,Object > dataModel)
    {
        if (dataModel.isEmpty())
        { return content; }

        StringReader reader = new StringReader(content);
        StringWriter sw = new StringWriter();
        try
        {
            //渲染模板
            String templateName = dataModel.getOrDefault("templateName", "generator").toString();
            Template template = new Template(templateName, reader, null, "UTF-8");
            template.process(dataModel, sw);
        }
        catch (Exception e)
        {
            throw new RuntimeException("渲染模板失败，请检查模板语法", e);
        }

        content = sw.toString();

        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(sw);

        return content;
    }
}